package util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 * @author Usuario
 *
 */
public class HibernateUtil {

	private static SessionFactory sessionFactory;

	
	/**
	 * Metodo para obter a sessao de conexao com o BD
	 */
	public static Session currentSession() {
		if (sessionFactory == null) {
			//Seta na variavel da Sessao do Hibernate o arquivo de configuracao
			//com o BD
			try {
				sessionFactory = new AnnotationConfiguration().configure(
						"hibernate.cfg.xml").buildSessionFactory();
			} catch (Exception e) {
				//Lancar erro
			}
			if (sessionFactory.isClosed()) {
				//Solicitar Instancia
				sessionFactory.openSession();
			}
		}

		return sessionFactory.getCurrentSession();
	}
}
